//
//  CCTestViewModel.m
//  Party
//
//  Created by its on 15/3/9.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCPartyViewModel.h"
#import "AFHTTPRequestOperationManager+RACSupport.h"
#import "CCPartyCellModel.h"
#import "CCPartyCellViewModel.h"

@interface CCPartyViewModel()
@property (strong, nonatomic) CCPartyModel *model;
@property (weak, nonatomic) RACDisposable *upDisposable;
@property (weak, nonatomic) RACDisposable *downDisposable;
@end

@implementation CCPartyViewModel
- (instancetype) initWithModel:(CCPartyModel *)model {
    if (self = [super init]) {
        _model = model;
        _viewTitleString = @"活动";
        _resultArray = [NSMutableArray array];
    }
    return self;
}

- (RACSignal *) upPull {
  RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [subscriber sendNext:nil];  //开始请求数据
    RACSignal *newSignal = [self.model requestAll];
    RACDisposable *signalDisposable = [newSignal subscribeNext:^(NSArray *x) {
      [_resultArray addObjectsFromArray: x];
    } error:^(NSError *error) {
      [subscriber sendError:error];
    } completed:^{
      [subscriber sendCompleted];
    }];
    RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
    [disposable addDisposable: signalDisposable];
    return disposable;
  }];
  return signal;
}

- (RACSignal *) downPull {
  RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    RACDisposable *signalDisposable = [[self.model requestOld] subscribeNext:^(NSArray *x) {
      [self.resultArray addObjectsFromArray: x];
    } error:^(NSError *error) {
      [subscriber sendError:error];
    } completed:^{
      [subscriber sendCompleted];
    }];
    
    RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
    [disposable addDisposable: signalDisposable];
    return disposable;
  }];
  return signal;
}

- (void) dealloc {
  NSLog(@"partyViewModel dealloc");
}
@end
